import Vue from 'vue'
import VueRouter from 'vue-router'

// 引入组件
import Layout from '@/views/layout/index.vue'
import Login from '@/views/login/index.vue'
import Dashboard from '@/views/dashboard/index.vue'
import Article from '@/views/article/index.vue'
import { getToken } from '@/utils/storage'

Vue.use(VueRouter)

// 配置路由规则
const routes = [
  // 一级路由
  // 登录页
  { path: '/login', component: Login },
  {
    // 布局组件
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      // 二级路由
      // 数据看板页
      { path: 'dashboard', component: Dashboard },
      // 文章管理页
      { path: 'article', component: Article }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, form, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (to.path == '/login') {
      next()
    }else{
      next('/login')
    }
  }
})

export default router